function askVerifyCode()
{
    post_without_token('http://localhost/auth/verify/send-code',{
        email: $("#input-email").val(),
        refer:1
    },function (data)
    {
        window.alert(data.reason)
    })
}
function askVerifyCode1()
{
    post_without_token('http://localhost/auth/verify/send-code',{
        email: $("#input-email").val()
        ,refer: 2
    },function (data)
    {
        window.alert(data.reason)
    })
}
function login()
{
    post_without_token('http://localhost/auth/verify/login',{
        username: $("#username").val(),
        password: $("#password").val(),
    },function (data){
        if(data.code===200)
        {
            localStorage.setItem('token',data.t.token)
            window.location="assignment.html"
       }
        else {
            alert(data.reason)
        }
    })

}
function loginById()
{
    post_without_token('http://localhost/auth/verify/login-by-id',{
        id: $("#id").val(),
        password: $("#password").val(),
    },function (data){
        if(data.code===200)
        {
            localStorage.setItem('token',data.t.token)
            window.location="assignment.html"
        }
        else {
            alert(data.reason)
        }
    })

}
function logout()
{
    get('http://localhost/auth/verify/logout',function (data){

        window.location="login.html"

    },function ()
    {
        window.location="login.html"
    })
}


function initUserInfo()
{

    get('http://localhost/data/request/index',function (data)
    {
        if(data.code===200)
        {

            $("#profile-name").text(data.reason);
        }
        else if(data.code==500)
        {
            alert(data.reason)
            window.location="login.html"
        }

    },function (){

    })
}


function register()
{
    if($("#password").val==$("#password1").val)
    {
        post_without_token('http://localhost/auth/verify/register',{
                username: $("#username").val(),
                password: $("#password").val(),
                email: $("#input-email").val(),
                verifyCode: $("#verifyCode").val(),
                id: $("#sid").val(),
            },
            function(data)
            {
                if(data.code===200)
                {
                    alert("注册成功！")
                    window.location='login.html'
                }
                else
                {
                    alert(data.reason)
                }
            })
    }
    else
        alert("您输入的两次密码不一致，请重新输入！")
}
function update()
{
    post_without_token('http://localhost/auth/verify/update',{
        username: $("#username").val(),
        password: $("#password").val(),
        email: $("#input-email").val(),
        verifyCode: $("#verifyCode").val(),
        id: $("#sid").val(),
    },function (data)
    {
        alert(data.reason)
        window.location("login.html")
    },function ()
    {
        alert("服务器无响应");
    })
}
function get(url, success,error){
    $.ajax({
        type: "get",
        url: url,
        async: true,
        dataType: 'json',
        beforeSend:function (xhr)
        {
            xhr.setRequestHeader("token",localStorage.getItem('token'))
        },
        xhrFields: {
            withCredentials: true
        },
        success: success,
        error: error
    });
}

function post(url, data, success,error){
    $.ajax({
        type: "post",
        url: url,
        async: true,
        data: data,
        dataType: 'json',
        beforeSend:function (xhr)
        {
            xhr.setRequestHeader("token",localStorage.getItem('token'))
        },
        xhrFields: {
            withCredentials: true
        },
        success: success,
        error: error
    });
}
function post_without_token(url, data, success,error){
    $.ajax({
        type: "post",
        url: url,
        async: true,
        data: data,
        dataType: 'json',

        xhrFields: {
            withCredentials: true
        },
        success: success,
        error: error
    });
}